package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.domain.entity.SysDept;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.DateUtils;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.framework.web.service.TokenService;
import com.zhuoyue.system.domain.po.SelectTimePo;
import com.zhuoyue.system.domain.po.TblAppUserPo;
import com.zhuoyue.system.domain.vo.AppUserPositionVo;
import com.zhuoyue.system.domain.vo.TblAppUserVo;
import com.zhuoyue.system.mapper.SysDeptMapper;
import com.zhuoyue.system.mapper.TblAppUserMapper;
import com.zhuoyue.system.service.ISysDeptService;
import com.zhuoyue.system.service.ITblAppUserService;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * APP用户Controller
 *
 * @author hewenze
 * @date 2020-08-05
 */
@RestController
@Api(value = "App用户接口", tags = "App用户操作接口")
@RequestMapping("/system/user1")
public class TblAppUserController extends BaseController {
    @Autowired
    private ITblAppUserService tblAppUserService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private TblAppUserMapper tblAppUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private OrgCodeUtils orgCodeUtils;

    @Autowired
    private ISysDeptService deptService;


    /**
     * 查询APP用户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询App用户列表", notes = "根据手机号或者用户名进行查询")
    public TableDataInfo list(TblAppUserPo tblAppUserPo, HttpServletRequest request) {
        tblAppUserPo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        startPage();
        List<TblAppUserVo> list = tblAppUserService.selectTblAppUserList(tblAppUserPo);
        return getDataTable(list);
    }

    /**
     * 根据账号和组织编码查询用户
     */
    @GetMapping("/getByAccountAndOrgCode")
    public AjaxResult getByAccountAndOrgCode(String account, String orgCode) {
        return AjaxResult.success(tblAppUserService.selectTblAppUserByAccountAndOrgCode(account, orgCode));
    }

    /**
     * 查询用户位置列表
     *
     * @param tblAppUserPo
     * @param request
     * @return
     */
    @GetMapping("/listUserPosition")
    @ApiOperation(value = "查询App用户定位", notes = "根据手机号进行查询")
    public TableDataInfo listUserPosition(TblAppUserPo tblAppUserPo, HttpServletRequest request) {
        tblAppUserPo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        startPage();
        List<AppUserPositionVo> list = tblAppUserService.selectAppUserPositionList(tblAppUserPo);
        return getDataTable(list);
    }

    /**
     * 导出APP用户列表
     */
    @Log(title = "APP用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出App用户列表", notes = "导出App用户列表")
    public AjaxResult export(TblAppUserPo tblAppUserPo, HttpServletRequest request) {
        tblAppUserPo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        tblAppUserPo.setPageNum(null);
        tblAppUserPo.setPageSize(null);
        List<TblAppUserVo> list = tblAppUserService.selectTblAppUserList(tblAppUserPo);
        ExcelUtil<TblAppUserVo> util = new ExcelUtil<TblAppUserVo>(TblAppUserVo.class);
        return util.exportExcel(list, "用户");
    }

    /**
     * 获取APP用户数量
     */
    @GetMapping(value = "AppUserCount")
    @ApiOperation(value = "获取APP用户数量", notes = "获取APP用户数量")
    public AjaxResult getAppUserCount(TblAppUserPo tblAppUserPo, HttpServletRequest request) {
        String orgCode = orgCodeUtils.getOrgCode(request);
        // 查询APP用户数量
        tblAppUserPo.setOrganizationCode(orgCode);

        // 查询昨日新增
        Date yesterdayTime = DateUtils.getYesterdayTime();
        Date yesterdayStart = DateUtils.getStartTimeOfDay(yesterdayTime);
        SelectTimePo selectTimePo = new SelectTimePo();
        selectTimePo.setSelectStartTime(yesterdayStart);
        selectTimePo.setSelectEndTime(yesterdayTime);
        selectTimePo.setOrgCode(orgCode);
        int yesterdayRegisterCount = tblAppUserService.getRegisterCount(selectTimePo);

        // 查询今日新增
        Date todayTime = new Date();
        Date todayStart = DateUtils.getStartTimeOfDay(todayTime);
        selectTimePo.setSelectStartTime(todayStart);
        selectTimePo.setSelectEndTime(todayTime);
        int todayRegisterCount = tblAppUserService.getRegisterCount(selectTimePo);

        // 若真实结果为0.5, 则为5000
        int increaseRate = 0;
        if (yesterdayRegisterCount != 0) {
            int subtractResult = todayRegisterCount - yesterdayRegisterCount;
            float floatResult = (float) subtractResult / (float) yesterdayRegisterCount;
            increaseRate = (int) (floatResult * 10000);
        }


        List<Integer> integers = tblAppUserService.selectTblAppUserCount(tblAppUserPo);
        integers.add(increaseRate);
        integers.add(yesterdayRegisterCount);
        integers.add(todayRegisterCount);

        return AjaxResult.success(integers);
    }

    @GetMapping(value = "getConditionAppUserCount")
    public AjaxResult getAppUserCountCondition(TblAppUserPo tblAppUserPo, Long deptId) {
        // 获取组织编码
        SysDept sysDept = deptService.selectDeptById(deptId);
        if (sysDept != null) {
            tblAppUserPo.setSelectedOrgCode(sysDept.getOrgCode());
        }
        if (!"1".equals(tblAppUserPo.getSourceType()) && !"2".equals(tblAppUserPo.getSourceType()) && !"all".equals(tblAppUserPo.getSourceType())) {
            tblAppUserPo.setSourceType(null);
        }
        if (!"0".equals(tblAppUserPo.getGender()) && !"1".equals(tblAppUserPo.getGender()) && !"2".equals(tblAppUserPo.getGender()) && !"all".equals(tblAppUserPo.getGender())) {
            tblAppUserPo.setGender(null);
        }
        return AjaxResult.success(tblAppUserMapper.selectAppUserCount(tblAppUserPo));
    }
}
